package com.example.ang.tripapp.activities;


import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RadioGroup;

import com.example.ang.tripapp.R;
import com.example.ang.tripapp.fragments.Fragment1;
import com.example.ang.tripapp.fragments.Fragment3;
import com.example.ang.tripapp.fragments.WeatherFragment;

import java.util.ArrayList;

/**
 * 主界面
 * zzg
 */
public class MainActivity extends FragmentActivity {
    public static final String UPDATE_WIDGET_WEATHER_ACTION = "com.way.action.update_weather";

    /**
     * 定义FragmentManager
     */
    private FragmentManager fm;
    /**
     * 定义上一个Fragment和当前的Fragment
     */
    private Fragment curFrag, lastFrag;
    /**
     * 定义Fragment集合
     */
    private ArrayList<Fragment> frags;
    /**
     * 定义RadioGroup
     */

    private RadioGroup rg;

    /**
     * 定义add按钮
     */
    private ImageView iv_btn, iv_add;

    /**
     * 定义PopubWindow
     */
    private PopupWindow pw;

    private DisplayMetrics metrics;

    private View contentView;

    private FrameLayout frameLayout;

    private ImageView iv_life_story, iv_long_trip;

    private Button btn_mine;

    private LinearLayout ll_check;
    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        findViews();
        initFragmentManager();
        setRGListener();
        setImageViewListener();
    }

    private void setImageViewListener() {
        iv_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Animation anim = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate);
                anim.setFillAfter(true);
                iv_add.startAnimation(anim);
                WindowManager.LayoutParams param = MainActivity.this.getWindow().getAttributes();
                param.alpha = 0.7f;
                getWindow().setAttributes(param);
                if (pw == null) {
                    metrics = new DisplayMetrics();
                    getWindowManager().getDefaultDisplay().getMetrics(metrics);
                    contentView = View.inflate(MainActivity.this, R.layout.mainactivity_popwindow, null);
                    iv_long_trip = (ImageView) contentView.findViewById(R.id.iv_long_trip);
                    iv_life_story = (ImageView) contentView.findViewById(R.id.iv_life_story);

                    pw = new PopupWindow(contentView, WindowManager.LayoutParams.WRAP_CONTENT,
                            WindowManager.LayoutParams.WRAP_CONTENT);
                    pw.setFocusable(true);
                    pw.setOutsideTouchable(true);
                    pw.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.transparent)));
                    pw.setAnimationStyle(R.style.my_pwstyle);
                    pw.setOnDismissListener(new PopupWindow.OnDismissListener() {

                        @Override
                        public void onDismiss() {
                            WindowManager.LayoutParams param = getWindow().getAttributes();
                            param.alpha = 1f;
                            getWindow().setAttributes(param);
                            Animation anim = AnimationUtils.loadAnimation(MainActivity.this, R.anim.revert_rotate);
                            anim.setFillAfter(true);
                            iv_add.startAnimation(anim);
                        }
                    });
                }
                pw.showAtLocation(frameLayout, Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, rg.getHeight());
//                iv_life_story.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_iv_lifestory));
//                iv_long_trip.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fade_iv_longtrip));
//                pw.showAsDropDown(frameLayout,0,0-pw.getHeight(), Gravity.CENTER_HORIZONTAL);
            }
        });
    }

    //设置Radiogroup监听
    private void setRGListener() {
        btn_mine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ll_check.setVisibility(View.VISIBLE);
                startActivity(new Intent(MainActivity.this, MineActivity.class));

            }
        });
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                switchFrags(checkedId);
            }

            private void switchFrags(int checkedId) {
                switch (checkedId) {
                    case R.id.rb_explore:
                        ll_check.setVisibility(View.VISIBLE);
                        switchfrag(1);
                        break;
                    case R.id.rb_cityhunter:
                        ll_check.setVisibility(View.GONE);
                        switchfrag(2);
                        break;
                    case R.id.rb_destination:
                        ll_check.setVisibility(View.VISIBLE);
                        switchfrag(3);
                        break;
                }
//                if (checkedId != 5) {
//                    curFrag = fm.findFragmentByTag(checkedId + "");
//                    if (curFrag == null) {
//                        if (checkedId < 5) {
//                            fm.beginTransaction().hide(lastFrag).commit();
//                            fm.beginTransaction().add(R.id.main_framelayout, frags.get(checkedId - 1), (checkedId) + "").commit();
//                            curFrag = frags.get(checkedId - 1);
//                        } else {
//                            fm.beginTransaction().hide(lastFrag).commit();
//                            Log.i("light", checkedId + "");
//                            fm.beginTransaction().add(R.id.main_framelayout, frags.get(checkedId - 4), (checkedId) + "").commit();
//                            curFrag = frags.get(checkedId - 4);
//                        }
//
//                    } else {
//                        fm.beginTransaction().hide(lastFrag).show(curFrag).commit();
//                    }
//                    lastFrag = curFrag;
//                }

            }

        });
    }

    private void switchfrag(int i) {
        curFrag = fm.findFragmentByTag(i + "");
        if (curFrag == null) {
        fm.beginTransaction().hide(lastFrag).commit();
        fm.beginTransaction().add(R.id.main_framelayout, frags.get(i-1), i+ "").commit();
        curFrag = frags.get(i-1);}
        else{
            fm.beginTransaction().hide(lastFrag).show(curFrag).commit();
        }
        lastFrag = curFrag;
    }

    //初始化FragmentManager
    private void initFragmentManager() {
        frags = new ArrayList<>();
        fm = getSupportFragmentManager();
        frags.add(new Fragment1());
        frags.add(new WeatherFragment());
        frags.add(new Fragment3());
//        frags.add(new Fragment4());
        fm.beginTransaction().add(R.id.main_framelayout, frags.get(0), 1 + "").commit();
        fm.beginTransaction().show(frags.get(0)).commit();
        lastFrag = frags.get(0);
        rg.check(R.id.rb_explore);
    }

    //找控件
    private void findViews() {
        rg = (RadioGroup) findViewById(R.id.rg);
        iv_btn = (ImageView) findViewById(R.id.iv_btn);
        iv_add = (ImageView) findViewById(R.id.iv_add);
        frameLayout = (FrameLayout) findViewById(R.id.main_framelayout);
        btn_mine = (Button) findViewById(R.id.rb_me);

        ll_check= (LinearLayout) findViewById(R.id.ll_search);
    }


}
